import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_demo/pages/download/domain/entity/country_bean.dart';
import 'package:get_demo/pages/download/presentation/controllers/MapDataController.dart';

class DownloadView extends GetView<MapDataController> {
  const DownloadView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print('First rebuild');
    return Container(
        color: Colors.white,
        child: SafeArea(
            child:
                Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Container(
              width: 650,
              height: 50,
              color: Color(0xFFBDBDBD),
              child: Row(
                children: [
                  IconButton(
                      onPressed: () {
                        controller.closePage();
                      },
                      icon: Icon(Icons.close_outlined)),
                  Expanded(
                      child: Container(
                    alignment: Alignment.center,
                    child: Text(
                      "地图下载",
                      style:
                          TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
                    ),
                  )),
                ],
              )),
          Container(
            margin: EdgeInsets.all(10),
          ),
          Row(
            children: [
              Container(
                margin: EdgeInsets.all(20),
                child: Text(
                  "行政区   ",
                  style: TextStyle(fontSize: 16),
                ),
              ),
              DropdownMenu<CountryBean>(
                width: 150,
                menuHeight: 200,
                hintText: "请选择省份",
                onSelected: (_data) {
                  controller.onSelect(_data?.code ?? "", _data?.name ?? "", 1);
                },
                dropdownMenuEntries: controller.buildProviceMenuList(),
              ),
              Container(
                width: 20,
              ),
              Obx(() => DropdownMenu<CountryBean>(
                    width: 150,
                    menuHeight: 200,
                    hintText: "请选择城市",
                    onSelected: (_data) {
                      controller.onSelect(
                          _data?.code ?? "", _data?.name ?? "", 2);
                    },
                    dropdownMenuEntries:
                        controller.cityList.value ?? List.empty(),
                  )),
              Container(
                width: 20,
              ),
              Obx(() => DropdownMenu<CountryBean>(
                    width: 150,
                    menuHeight: 200,
                    hintText: "请选择区/县",
                    onSelected: (_data) {
                      controller.onSelect(
                          _data?.code ?? "", _data?.name ?? "", 3);
                    },
                    dropdownMenuEntries:
                        controller.countyList.value ?? List.empty(),
                  )),
              Container(
                width: 20,
              ),
            ],
          ),
          Row(children: [
            Container(
              margin: EdgeInsets.all(20),
              child: Text(
                "保存路径",
                style: TextStyle(fontSize: 16),
              ),
            ),
            Container(
              width: 400,
              height: 30,
              padding: EdgeInsets.only(left: 20, right: 20),
              decoration: BoxDecoration(
                  border: Border.all(color: Color(0xFF333333), width: 1),
                  borderRadius: BorderRadius.circular(2)),
              child: Obx(() => Text(
                    controller.params.value.saveDir,
                    style: TextStyle(fontSize: 16),
                  )),
            ),
            IconButton(
                onPressed: () {
                  controller.selectDir();
                },
                icon: Icon(Icons.more_horiz))
          ]),
          Row(children: [
            Container(
              margin: EdgeInsets.all(20),
              child: Text(
                "图片格式",
                style: TextStyle(fontSize: 16),
              ),
            ),
            DropdownMenu<String>(
              width: 150,
              menuHeight: 200,
              hintText: controller.buildPathType().first.label,
              onSelected: (_data) {},
              dropdownMenuEntries: controller.buildPathType(),
            ),
            Container(
              width: 20,
            ),
            DropdownMenu<String>(
              width: 150,
              menuHeight: 200,
              hintText: "jpg",
              onSelected: (_data) {},
              dropdownMenuEntries: controller.buildImageType(),
            ),
          ]),
          Row(children: [
            Container(
              margin: EdgeInsets.all(20),
              child: Text(
                "地图级别",
                style: TextStyle(fontSize: 16),
              ),
            ),
            DropdownMenu<int>(
              width: 80,
              menuHeight: 200,
              hintText: "1",
              onSelected: (_data) {},
              dropdownMenuEntries: controller.buildMinList(),
            ),
            Container(
              width: 20,
              child: Text(
                "-",
                style: TextStyle(fontSize: 16),
              ),
            ),
            DropdownMenu<int>(
              width: 80,
              menuHeight: 200,
              hintText: "15",
              onSelected: (_data) {},
              dropdownMenuEntries: controller.buildMaxList(),
            ),
          ]),
          Row(
            children: [
              ElevatedButton(
                onPressed: () {
                  controller.stopMapDownloadTask();
                },
                child: const Text('Stop task'),
              ),
              Container(
                height: 50,
              ),
              ElevatedButton(
                onPressed: () {
                  controller.puaseTask();
                },
                child: const Text('Pause task'),
              ),
              Container(
                height: 50,
              ),
              ElevatedButton(
                onPressed: () {
                  controller.resumeTask();
                },
                child: const Text('Resume task'),
              ),
              Container(
                height: 50,
              ),
              ElevatedButton(
                onPressed: () {
                  controller.startDownload(MapDownloadParams(
                      mapUrl:
                          "https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}.png",
                      saveDir: "C:/MapDownload/Google Map/changanqu",
                      west: 108.6338137,
                      east: 109.2190777,
                      north: 34.3060843,
                      south: 34.0499605));
                  // Get.toNamed('/second?id=123');
                },
                child: const Text('start download'),
              ),
            ],
          ),
          Obx(() {
            return Container(
              child: Text(
                  "${controller.getData().value != null ? controller.getData().value!.toView() : "等待下载中"}"),
            );
          })
        ])));
  }
}
